/*
 * mdl.h -- mdl model loader
 *
 * Copyright (c) 2005-2007 David HENRY
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef _MDL_H
#define _MDL_H

#ifdef __cplusplus
extern "C" {
#endif

#include "../../math/src/mgpi_math.h"

    /* Vector */
    typedef float vec3_t[3];

#pragma pack(push,1)

    /* MDL header */
    struct _mdl_header {
        int ident; /* magic number: "IDPO" */
        int version; /* version: 6 */

        vec3_t scale; /* scale factor */
        vec3_t translate; /* translation vector */
        float boundingradius;
        vec3_t eyeposition; /* eyes' position */

        int num_skins; /* number of textures */
        int skinwidth; /* texture width */
        int skinheight; /* texture height */

        int num_verts; /* number of vertices */
        int num_tris; /* number of triangles */
        int num_frames; /* number of frames */

        int synctype; /* 0 = synchron, 1 = random */
        int flags; /* state flag */
        float size;
    };

    /* Skin */
    struct _mdl_skin {
        int group; /* 0 = single, 1 = group */
        GLubyte *data; /* texture data */
    };

    /* Texture coords */
    struct _mdl_texcoord {
        int onseam;
        int s;
        int t;
    };

    /* Triangle info */
    struct _mdl_triangle {
        int facesfront; /* 0 = backface, 1 = frontface */
        int vertex[3]; /* vertex indices */
    };

    /* Compressed vertex */
    struct _mdl_vertex {
        unsigned char v[3];
        unsigned char normalIndex;
    };

    /* Simple frame */
    struct mdl_simpleframe_t {
        struct _mdl_vertex bboxmin; /* bouding box min */
        struct _mdl_vertex bboxmax; /* bouding box max */
        char name[16];
        struct _mdl_vertex *verts; /* vertex list of the frame */
    };

    /* Model frame */
    struct _mdl_frame {
        int type; /* 0 = simple, !0 = group */
        struct mdl_simpleframe_t frame; /* this program can't read models
				      composed of group frames! */
    };

    /* MDL model structure */
    struct _mdl_model {
        struct _mdl_header header;

        struct _mdl_skin *skins;
        struct _mdl_texcoord *texcoords;
        struct _mdl_triangle *triangles;
        struct _mdl_frame *frames;

        GLuint *tex_id;
        int iskin;
    };

#pragma pack(pop)

    typedef struct _mdl_model mdl_model_t;

    GLuint MGPI_API gsubMakeTextureFromSkin(int, const struct _mdl_model *);
    int MGPI_API gsubReadMDLModel(const char *, struct _mdl_model *);
    void MGPI_API gsubFreeMDLModel(struct _mdl_model *);
    void MGPI_API gsubRenderMDLFrame(int, const struct _mdl_model *);
    void MGPI_API gsubRenderMDLFrameItp(int, float, const struct _mdl_model *);
    void MGPI_API gsubAnimateMDL(int, int, int *, float *);

#ifdef __cplusplus
}
#endif

#endif
